package com.lifeverse.repository;

import com.lifeverse.entity.Decision;
import com.lifeverse.entity.DecisionFeedback;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 决策反馈数据访问接口
 */
@Repository
public interface DecisionFeedbackRepository extends JpaRepository<DecisionFeedback, Long> {
    
    /**
     * 根据决策查找反馈
     */
    List<DecisionFeedback> findByDecision(Decision decision);
    
    /**
     * 根据决策查找反馈（分页）
     */
    Page<DecisionFeedback> findByDecision(Decision decision, Pageable pageable);
    
    /**
     * 根据反馈类型查找反馈
     */
    List<DecisionFeedback> findByFeedbackType(String feedbackType);
    
    /**
     * 根据反馈来源查找反馈
     */
    List<DecisionFeedback> findByFeedbackSource(String feedbackSource);
    
    /**
     * 根据是否为正面反馈查找
     */
    List<DecisionFeedback> findByIsPositive(Boolean isPositive);
    
    /**
     * 根据处理状态查找反馈
     */
    List<DecisionFeedback> findByIsProcessed(Boolean isProcessed);
    
    /**
     * 查找未处理的反馈
     */
    List<DecisionFeedback> findByIsProcessedFalse();
    
    /**
     * 查找高影响反馈
     */
    @Query("SELECT df FROM DecisionFeedback df WHERE df.impactLevel >= :impactThreshold")
    List<DecisionFeedback> findHighImpactFeedback(@Param("impactThreshold") BigDecimal impactThreshold);
    
    /**
     * 根据评分范围查找反馈
     */
    @Query("SELECT df FROM DecisionFeedback df WHERE df.rating BETWEEN :minRating AND :maxRating")
    List<DecisionFeedback> findByRatingRange(@Param("minRating") BigDecimal minRating,
                                           @Param("maxRating") BigDecimal maxRating);
    
    /**
     * 根据时间范围查找反馈
     */
    List<DecisionFeedback> findByFeedbackTimeBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 根据决策按时间倒序查找反馈
     */
    List<DecisionFeedback> findByDecisionOrderByFeedbackTimeDesc(Decision decision);
    
    /**
     * 查找最近的反馈
     */
    @Query("SELECT df FROM DecisionFeedback df WHERE df.decision = :decision ORDER BY df.feedbackTime DESC")
    List<DecisionFeedback> findRecentFeedback(@Param("decision") Decision decision, Pageable pageable);
    
    /**
     * 统计决策的反馈数量
     */
    long countByDecision(Decision decision);
    
    /**
     * 统计正面反馈数量
     */
    long countByDecisionAndIsPositive(Decision decision, Boolean isPositive);
    
    /**
     * 统计特定类型的反馈数量
     */
    long countByFeedbackType(String feedbackType);
    
    /**
     * 统计未处理的反馈数量
     */
    long countByIsProcessedFalse();
    
    /**
     * 计算决策的平均评分
     */
    @Query("SELECT AVG(df.rating) FROM DecisionFeedback df WHERE df.decision = :decision AND df.rating IS NOT NULL")
    BigDecimal calculateAverageRating(@Param("decision") Decision decision);
    
    /**
     * 计算决策的平均影响程度
     */
    @Query("SELECT AVG(df.impactLevel) FROM DecisionFeedback df WHERE df.decision = :decision AND df.impactLevel IS NOT NULL")
    BigDecimal calculateAverageImpactLevel(@Param("decision") Decision decision);
    
    /**
     * 查找包含特定关键词的反馈
     */
    @Query("SELECT df FROM DecisionFeedback df WHERE df.feedbackContent LIKE %:keyword% OR df.improvementSuggestions LIKE %:keyword%")
    List<DecisionFeedback> findByKeyword(@Param("keyword") String keyword);
    
    /**
     * 查找需要紧急处理的反馈
     */
    @Query("SELECT df FROM DecisionFeedback df WHERE df.isProcessed = false AND " +
           "(df.impactLevel >= :urgentThreshold OR df.rating <= :lowRatingThreshold)")
    List<DecisionFeedback> findUrgentFeedback(@Param("urgentThreshold") BigDecimal urgentThreshold,
                                            @Param("lowRatingThreshold") BigDecimal lowRatingThreshold);
}